//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.poi.openxml4j.opc.internal.marshallers;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Iterator;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.StreamHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.openxml4j.opc.internal.PartMarshaller;
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public final class ZipPartMarshaller implements PartMarshaller {
    private static final POILogger logger = POILogFactory.getLogger(ZipPartMarshaller.class);

    public ZipPartMarshaller() {
    }

    public boolean marshall(PackagePart part, OutputStream os) throws OpenXML4JException {
        if (!(os instanceof ZipArchiveOutputStream)) {
            logger.log(7, new Object[]{"Unexpected class " + os.getClass().getName()});
            throw new OpenXML4JException("ZipOutputStream expected !");
        } else if (part.getSize() == 0L && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
            return true;
        } else {
            ZipArchiveOutputStream zos = (ZipArchiveOutputStream)os;
            ZipArchiveEntry partEntry = new ZipArchiveEntry(ZipHelper.getZipItemNameFromOPCName(part.getPartName().getURI().getPath()));

            try {
                zos.putArchiveEntry(partEntry);

                try {
                    InputStream ins = part.getInputStream();
                    Throwable var6 = null;

                    try {
                        IOUtils.copy(ins, zos);
                    } catch (Throwable var24) {
                        var6 = var24;
                        throw var24;
                    } finally {
                        if (ins != null) {
                            if (var6 != null) {
                                try {
                                    ins.close();
                                } catch (Throwable var23) {
                                    var6.addSuppressed(var23);
                                }
                            } else {
                                ins.close();
                            }
                        }

                    }
                } finally {
                    zos.closeArchiveEntry();
                }
            } catch (IOException var27) {
                logger.log(7, new Object[]{"Cannot write: " + part.getPartName() + ": in ZIP", var27});
                return false;
            }

            if (part.hasRelationships()) {
                PackagePartName relationshipPartName = PackagingURIHelper.getRelationshipPartName(part.getPartName());
                marshallRelationshipPart(part.getRelationships(), relationshipPartName, zos);
            }

            return true;
        }
    }

    public static boolean marshallRelationshipPart(PackageRelationshipCollection rels, PackagePartName relPartName, ZipArchiveOutputStream zos) {
        Document xmlOutDoc = DocumentHelper.createDocument();
        Element root = xmlOutDoc.createElementNS("http://schemas.openxmlformats.org/package/2006/relationships", "Relationships");
        xmlOutDoc.appendChild(root);
        URI sourcePartURI = PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(relPartName.getURI());

        Element relElem;
        String targetValue;
        for(Iterator var6 = rels.iterator(); var6.hasNext(); relElem.setAttribute("Target", targetValue)) {
            PackageRelationship rel = (PackageRelationship)var6.next();
            relElem = xmlOutDoc.createElementNS("http://schemas.openxmlformats.org/package/2006/relationships", "Relationship");
            root.appendChild(relElem);
            relElem.setAttribute("Id", rel.getId());
            relElem.setAttribute("Type", rel.getRelationshipType());
            URI uri = rel.getTargetURI();
            if (rel.getTargetMode() == TargetMode.EXTERNAL) {
                targetValue = uri.toString();
                relElem.setAttribute("TargetMode", "External");
            } else {
                URI targetURI = rel.getTargetURI();
                targetValue = PackagingURIHelper.relativizeURI(sourcePartURI, targetURI, true).toString();
            }
        }

        xmlOutDoc.normalize();
        ZipArchiveEntry ctEntry = new ZipArchiveEntry(ZipHelper.getZipURIFromOPCName(relPartName.getURI().toASCIIString()).getPath());

        try {
            zos.putArchiveEntry(ctEntry);

            boolean var18;
            try {
                var18 = StreamHelper.saveXmlInStream(xmlOutDoc, zos);
            } finally {
                zos.closeArchiveEntry();
            }

            return var18;
        } catch (IOException var16) {
            logger.log(7, new Object[]{"Cannot create zip entry " + relPartName, var16});
            return false;
        }
    }
}
